package com.zetor.ms.facade.controller;

import com.zetor.ms.application.service.base.ITransactionService;
import com.zetor.ms.core.base.BaseController;
import com.zetor.ms.core.bo.Result;
import com.zetor.ms.core.util.LogUtils;
import com.zetor.ms.facade.vo.DemoParamVO;
import io.seata.core.context.RootContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 事务 demo controller
 *
 * @author zlc
 * @date 6.30
 */
@RestController
public class TransactionController extends BaseController {
    @Autowired
    private ITransactionService demoService;

    /**
     * 分布式事务 seata
     * case3: client a -> client b， client b异常
     * 全局事务rollback
     * @return
     */
    @PostMapping("/v1/seata/clientc/case4")
    public Result seataCase4(@RequestBody DemoParamVO param) {
        LogUtils.info("SestCase4--XID:" + RootContext.getXID());
        Long id = demoService.seataCase4(Long.parseLong(param.getId()));
        if (id != 0L) {
            return result("info.logic.success", id);
        }
        return result("error.logic.failed");
    }

    /**
     * 分布式事务 seata
     * 写保护测试
     * @return
     */
    @PostMapping("/v1/seata/clientc/case6")
    public Result seataCase6(@RequestBody DemoParamVO param) {
        LogUtils.info("SestCase6--XID:" + RootContext.getXID());
        demoService.seataCase6(Long.parseLong(param.getId()));
        return result("info.logic.success");
    }



    /**
     * 分布式事务 seata
     * 读未提交效果
     * @return
     */
    @PostMapping("/v1/seata/clientc/case8")
    public Result seataCase8(@RequestBody DemoParamVO param) {
        LogUtils.info("SestCase8--XID:" + RootContext.getXID());
        return result("info.logic.success", demoService.seataCase8(Long.parseLong(param.getId())));
    }

}
